<template>
  <div style="overflow: hidden;height:calc(100% - 38px)">
    <div class="jurisdictionItem">
      <div class="jurisdictionTitle">查看设备</div>
      <newmultipleSelection ref="viewDeviceSelection" :dataList="nowSeviceList" style="height: 320px;"
        :selectionType="selectionType" :getIdMethod="item => item.id"></newmultipleSelection>
    </div>
    <div class="jurisdictionItem">
      <div class="jurisdictionTitle">操作设备</div>
      <newmultipleSelection ref="editDeviceSelection" :dataList="nowSeviceList" style="height: 320px;"
        :selectionType="selectionType" :getIdMethod="item => item.id"></newmultipleSelection>
    </div>
  </div>
</template>

<script>
  import multipleSelection from '@/components/module/selection/base/multipleSelection.vue'
  import newmultipleSelection from '@/components/module/selection/base/newmultipleSelection.vue'
  import userManagerStore from '@/page/userManager/userManagerStore.js'
  export default {
    components: {
      multipleSelection,
      newmultipleSelection
    },
    props: {

    },
    data () {
      return {
        selectionType: 'multiple',
        responData: [],
      }
    },
    created () {},
    mounted () {

    },
    destroyed () {

    },
    computed: {
      user () {
        return userManagerStore.state.nowUser
      },
      unit () {
        return userManagerStore.state.nowUnit
      },
      nowSeviceList () {
        // console.log("操作设备",userManagerStore.state.nowUnitDeviceTree);
        return [userManagerStore.state.nowUnitDeviceTree]
      },
      // nowViewCheckedIds() {
      // 	return userManagerStore.state.nowUserViewDeviceIds
      // },
      // nowEditCheckedIds() {
      // 	return userManagerStore.state.nowUserEditDeviceIds
      // },
      nowViewCheckedIds: {
        get: function() {
          return userManagerStore.state.nowUserViewDeviceIds
        },
        set: function(newValue) {
          return userManagerStore.state.nowUserViewDeviceIds=newValue
        }
      },
      nowEditCheckedIds: {
        get: function() {
          return userManagerStore.state.nowUserEditDeviceIds
        },
        set: function(newValue) {
          return userManagerStore.state.nowUserEditDeviceIds=newValue
        }
      },
      a () {
        return "调用"
      },
      viewNeedSave () {
        // console.log("viewNeedSave变化");
        var viewDevice=[]
        var viewKeys=Object.keys(this.$refs.viewDeviceSelection.nowSelectIds)
        for(let i=0;i<viewKeys.length;i++) {
          if(this.$refs.viewDeviceSelection.nowSelectIds[viewKeys[i]]) {
            viewDevice.push(viewKeys[i])
          }
        }

        if(this.nowViewCheckedIds.length==viewDevice.length) {
          for(let i=0;i<viewDevice.length;i++) {
            if(this.nowViewCheckedIds.indexOf(parseFloat(viewDevice[i]))<0) {
              return true
            }
          }
          return false
        } else {
          return true
        }
      },
      editNeedSave () {
        var editDevice=[]
        var editKeys=Object.keys(this.$refs.editDeviceSelection.nowSelectIds)
        for(let i=0;i<editKeys.length;i++) {
          if(this.$refs.editDeviceSelection.nowSelectIds[editKeys[i]]) {
            editDevice.push(editKeys[i])
          }
        }

        if(this.nowEditCheckedIds.length==editDevice.length) {
          for(let i=0;i<editDevice.length;i++) {
            if(this.nowEditCheckedIds.indexOf(parseFloat(editDevice[i]))<0) {
              return true
            }
          }
          return false
        } else {
          return true
        }
      }
    },
    watch: {
      nowViewCheckedIds () {
        this.resetDatas()
      },
      responData () {
        if(this.responData.length>=2) {
          // console.log("侦听器");
          if(this.responData[0].status!=0&&this.responData[1].status!=0) {
            if(this.responData[0].status!=-1&&this.responData[1].status!=-1) {
              this.$dialogMethod.showMessage('设备权限保存成功')
              this.resetCheckedIds()
            } else if(!this.responData[0].status==-1||!this.responData[1].status==-1) {
              this.$dialogMethod.showMessage('设备权限保存失败')
            }
          }
          this.responData=[]
        }
      }
    },
    methods: {
      resetDatas () {
        this.$refs.viewDeviceSelection.setNowCheckedIds(this.nowViewCheckedIds)
        this.$refs.editDeviceSelection.setNowCheckedIds(this.nowEditCheckedIds)
      },
      saveViewDevice () {
        // console.log("viewNeedSave",this.viewNeedSave);
        // if(!this.viewNeedSave){
        // 	this.responData.push({status:0,message:'无需保存'})
        // 	return
        // }
        var viewDeviceArr=[]
        var keys=Object.keys(this.$refs.viewDeviceSelection.nowSelectIds)
        for(let i=0;i<keys.length;i++) {
          if(this.$refs.viewDeviceSelection.nowSelectIds[keys[i]]) {
            viewDeviceArr.push(keys[i])
          }
        }
        this.$api.userApi.bindUserAndDevice('view',this.user.id,viewDeviceArr.join(','),res => {
          this.responData.push({status: 1,message: res})
          this.$dialogMethod.showMessage(res)

        },failure => {
          this.responData.push({status: -1,message: failure})
          this.$dialogMethod.showMessage(failure)
        })
      },
      saveEditDevice () {

        // var editDeviceArr = []
        // var keys = Object.keys(this.$refs.editDeviceSelection.nowSelectIds)
        // for(let i = 0;i < keys.length;i++){
        // 	if(this.$refs.editDeviceSelection.nowSelectIds[keys[i]]){
        // 		editDeviceArr.push(keys[i])
        // 	}
        // }
        var editDeviceStr=this.$refs.editDeviceSelection.checkNodesIdStr
        this.$api.userApi.bindUserAndDevice('edit',this.user.id,editDeviceStr,res => {
          this.responData.push({status: 1,message: res})

          this.$dialogMethod.showMessage(res)
        },failure => {
          this.responData.push({status: -1,message: failure})
          this.$dialogMethod.showMessage(failure)
        })
      },
      saveEdit () {
        this.saveViewDevice()
        this.saveEditDevice()
      },
      resetCheckedIds () {
        if(this.viewNeedSave) {
          var viewDevice=[]
          var viewKeys=Object.keys(this.$refs.viewDeviceSelection.nowSelectIds)
          for(let i=0;i<viewKeys.length;i++) {
            if(this.$refs.viewDeviceSelection.nowSelectIds[viewKeys[i]]) {
              viewDevice.push(viewKeys[i])
            }
          }
          this.nowViewCheckedIds=viewDevice
        }

        if(this.editNeedSave) {
          var editDevice=[]
          var editKeys=Object.keys(this.$refs.editDeviceSelection.nowSelectIds)
          for(let i=0;i<editKeys.length;i++) {
            if(this.$refs.editDeviceSelection.nowSelectIds[editKeys[i]]) {
              editDevice.push(editKeys[i])
            }
          }
          this.nowEditCheckedIds=editDevice
        }
      }
    }
  }
</script>

<style>
  .jurisdictionItem {
    border: solid 1px #E4E4E4;
    box-sizing: border-box;
    float: left;
    margin-right: 30px;
    width: calc(50% - 15px);
  }

  .jurisdictionItem:last-child {
    margin-right: 0;
  }

  .jurisdictionItem .list-body {
    border: none;
  }

  /* .jurisdictionTitle{
		font-size: 14px;
		color: #666666;
		line-height: 20px;
		background: #E3E7EC;
		text-align: center;
		line-height: 30px;
	} */
</style>